package org.rcloud.io.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.rcloud.io.Utils.LikeVo;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.PersonalImage;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.mapper.ArticleMapper;
import org.rcloud.io.mapper.ShootLikeMapper;
import org.rcloud.io.mapper.ShootMapper;
import org.rcloud.io.mapper.UserMapper;
import org.rcloud.io.model.Article;
import org.rcloud.io.model.Shoot;
import org.rcloud.io.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/2/26.
 */
@Service(value = "ArticleService")
public class ArticleServiceImpl implements ArticleService{
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShootLikeMapper shootLikeMapper;

    @Autowired
    private ShootMapper shootMapper;

    @Override
    public ResultVo save(Article article) {

        if (article.equals("") || article == null){
            return ResultVo.setError("数据为空");
        } else {
            try {
                int count = articleMapper.insertSelective(article);
                return ResultVo.setOK(count);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }
    }

    @Override
    public ResultVo findTitle(String nameTitle) {

        if (nameTitle.equals("") || nameTitle == null){
            return ResultVo.setError("数据为空");
        } else {
            try {
                String s = articleMapper.selectByTitle(nameTitle).getId().toString();
                return ResultVo.setOK(s);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }
    }

    @Override
    public PageVo<Article> findRuralNoBinding(Integer rId) {
        try {
            List<Article> list = articleMapper.findRuralNoBinding(rId);
            if (list.equals("") || list == null) {
                return PageVo.setError("查询数据为空");
            } else {
                return PageVo.setOk(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }
    }

    @Override
    public PageVo<Article> findRuralYesBinding(Integer rId) {
        try {
            List<Article> list = articleMapper.findRuralYesBinding(rId);
            if (list.equals("") || list == null) {
                return PageVo.setError("查询数据为空");
            } else {
                return PageVo.setOk(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }
    }

    @Override
    public PageVo<Article> findSpecialtyNoBinding(Integer bId) {
        try {
            List<Article> list = articleMapper.findSpecialtyNoBinding(bId);
            if (list.equals("") || list == null) {
                return PageVo.setError("查询数据为空");
            } else {
                return PageVo.setOk(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }
    }

    @Override
    public PageVo<Article> findSpecialtyYesBinding(Integer bId) {
        try {
            List<Article> list = articleMapper.findSpecialtyYesBinding(bId);
            if (list.equals("") || list == null) {
                return PageVo.setError("查询数据为空");
            } else {
                return PageVo.setOk(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }
    }

    @Override
    public PageInfo<Article> selectAll(int pageNum, int pageSize, Map map) {
        PageHelper.startPage(pageNum,pageSize,true);
        List<Article> articleList = articleMapper.selectAll(map);
        PageInfo<Article> pageInfo = new PageInfo<>(articleList);
        return pageInfo;
    }

    @Override
    public ResultVo selectLikeSum(Integer pageNum,Integer pageSize, Integer categoryId) {
        PageHelper.startPage(pageNum,pageSize);
        List<LikeVo> list = articleMapper.selectSum(categoryId);
        PageInfo<LikeVo> pageInfo = new PageInfo<>(list);
        return ResultVo.setOK(pageInfo);
    }

    @Override
    public PageVo<Article> selectVoteNum(JSONObject record) {
        try {
            List<Article> articleList = articleMapper.selectVoteNum(record);
            return PageVo.setOk(articleList);
        } catch (Exception e) {
            e.printStackTrace();
            return PageVo.setError(e.getMessage());
        }
    }


    /**
     * 根据类别id查询相关文章
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @return
     */
    @Override
    public ResultVo selectByCategoryId(Integer pageNum,Integer pageSize, Integer categoryId) {
        PageHelper.startPage(pageNum,pageSize);
        List<LikeVo> likeVos = articleMapper.selectByCategoryId(categoryId);
        PageInfo<LikeVo> pageInfo = new PageInfo<>(likeVos);
        return ResultVo.setOK(pageInfo);
    }

    /**
     * 查询个人点赞过的作品
     * @param pageNum
     * @param pageSize
     * @param id
     * @return
     */
    @Override
    public ResultVo selectByUserId(Integer pageNum, Integer pageSize, Integer id) {
        PageHelper.startPage(pageNum,pageSize);
        List<LikeVo> likeVos = articleMapper.selectByUserId(id);
        PageInfo<LikeVo> pageInfo = new PageInfo<>(likeVos);
        return ResultVo.setOK(pageInfo);
    }

    /**
     * 根据图片文章id，查询作者相关作品
     * @param articleId
     * @return
     */
    @Override
    public ResultVo selectAllByArticleId(Integer articleId) {
        PersonalImage personalImage = userMapper.selectByArticleId(articleId);
        /*List<Shoot> shoots = shootMapper.selectByArticleId(articleId);
        Shoot shoot = shoots.get(shoots.size() - 1);
        personalImage.setNickname(shoot.getNickname());
        personalImage.setMyself(shoot.getMyselfintrod());*/
        List<LikeVo> list = articleMapper.selectAllByUserId(personalImage.getId());
        personalImage.setList(list);
        return ResultVo.setOK(personalImage);
    }

    /**
     * 根据openID查询用户相关作品
     * @param openId
     * @return
     */
    @Override
    public ResultVo selectAllByOpenId(String openId) {
        PersonalImage personalImage = userMapper.selectByOpenid(openId);
        List<LikeVo> list = articleMapper.selectAllByUserId(personalImage.getId());
        personalImage.setList(list);
        return ResultVo.setOK(personalImage);
    }

//  分页
//    @Override
//    public PageInfo<Article> selectVoteNum(int pageNum, int pageSize, Map map){
//        PageHelper.startPage(pageNum,pageSize,true);
//        List<Article> articleList = articleMapper.selectVoteNum(map);
//        PageInfo<Article> pageInfo = new PageInfo<>(articleList);
//        return pageInfo;
//    }
}
